package com.ro.model.blog;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Rory
 */
public class AuthInterceptor implements Interceptor {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void intercept(Invocation inv) {
        boolean authFlag = false;
        String sessionAuthToken = "";
        Controller c = inv.getController();
        String authToken = c.getCookie("ISL");
        try {
            sessionAuthToken = Redis.use("ec").get("authToken");
            authFlag = Redis.use("ec").get("auth");
        } catch (Exception e) {
            logger.log(Level.INFO, "读缓存有问题,可能没初始化!");
        }
        if (authFlag && (sessionAuthToken.equals(authToken))) {
            c.setAttr("ISL", 1);
            inv.invoke();
        } else {
            inv.getController().renderError(404);
        }
    }
}
